import { defineStore } from "pinia";

const useStore = defineStore({
  id: "capacity",
  state: () => ({
    recordList: [],
    areaLayout: [],
    areaTypes: [],
    capacityLayout: [],
    weatherOptions: {},
    selectedRecordIndex: -1,
    // selectedRecordStatus: -2,
    historyRecords: [], //历史记录
    historyQueryOptions: { setted: false }, //历史记录查询条件
  }),
  getters: {
    selectedRecord() {
      if (this.recordList.length == 0) {
        this.selectedRecordIndex = -1;
      } else {
        while (this.selectedRecordIndex >= this.recordList.length) {
          this.selectedRecordIndex--;
        }
      }
      if (this.selectedRecordIndex == -1) {
        return {
          content: this.areaLayout,
          end: "2077-12-31 23:59:59",
          number: -1,
          stamp: "2022-01-01 00:00:00",
          start: "2022-01-01 00:00:00",
        };
      }
      return this.recordList[this.selectedRecordIndex];
    },
    selectedRecordStatus() {
      return this.checkStatus(-1);
    },
  },
  actions: {
    setSelectedRecordIndex(payload) {
      this.selectedRecordIndex = payload;
    },
    setRecord(payload) {
      this.recordList = payload;
    },
    checkStatus(index = -1) {
      let item = this.selectedRecord;
      if (index != -1) {
        item = this.recordList[index];
      }
      let status = 0; // 0: ineffective
      if (item.number == -1) {
        status = 2; // 2: default record
      } else if (new Date(item.end).getTime() < new Date().getTime()) {
        status = -1; // -1: expired
      } else if (new Date(item.start).getTime() < new Date().getTime()) {
        status = 1; // 1: effective
      }
      // console.log('checkItemStatus = ' + status, item);
      return status;
    },
    computedTotalCapacity(payload = -1, history = false) {
      let content;
      let areaTypes = this.areaTypes;
      if (payload == -1) {
        content = this.selectedRecord.content;
      } else {
        if (history) {
          content = this.historyRecords[payload].content;
        } else {
          content = this.recordList[payload].content;
        }
      }
      // console.log(content);
      // console.log(areaTypes);
      if (!content || !areaTypes)
        return { value: 0, valueFull: 0, details: [] };
      let capacity = { value: 0, valueFull: 0, details: [] };
      for (let i = 0; i < areaTypes.length; i++) {
        let cell = {
          areaType: areaTypes[i].areaType,
          areaTypeName: areaTypes[i].areaTypeName,
          totalCapacity: areaTypes[i].totalCapacity,
          totalCapacityDiscounted: areaTypes[i].totalCapacity,
          originalCapacityAccumulated: 0,
          currentCapacityAccumulated: 0,
          discountingRatio: 1,
          limitedCapacityFull: 0,
          limitedCapacity: 0,
        };
        capacity.details.push(cell);
      }
      for (let i = 0; i < content.length; i++) {
        let corn = content[i];
        for (let j = 0; j < capacity.details.length; j++) {
          let cell = capacity.details[j];
          if (cell.areaType === corn.areaType) {
            cell.currentCapacityAccumulated += corn.currentCapacity;
            cell.originalCapacityAccumulated += corn.originalCapacity;
            if (cell.totalCapacity == 0) {
              cell.limitedCapacity = cell.currentCapacityAccumulated;
              cell.limitedCapacityFull = cell.originalCapacityAccumulated;
            } else {
              cell.discountingRatio =
                cell.currentCapacityAccumulated /
                cell.originalCapacityAccumulated;
              cell.totalCapacityDiscounted = Math.round(
                cell.totalCapacity * cell.discountingRatio
              );
              cell.limitedCapacity = Math.min(
                cell.currentCapacityAccumulated,
                cell.totalCapacityDiscounted
              );
              cell.limitedCapacityFull = Math.min(
                cell.originalCapacityAccumulated,
                cell.totalCapacity
              );
            }
          }
        }
      }
      capacity.value = 9999;
      capacity.valueFull = 9999;
      for (let i = 0; i < capacity.details.length; i++) {
        capacity.value = Math.min(
          capacity.value,
          capacity.details[i].limitedCapacity
        );
        capacity.valueFull = Math.min(
          capacity.valueFull,
          capacity.details[i].limitedCapacityFull
        );
      }
      return capacity;
    },
  },
});

export default useStore;
